package com.ytjj.qmyx.mall.controller.h5;

import com.ytjj.common.api.CommonResult;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.utils.Aes128CBCUtil;
import com.ytjj.qmyx.mall.model.request.QueryOrdersRequest;
import com.ytjj.qmyx.mall.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/h5OrderController")
@Slf4j
public class H5OrderController extends BaseController {
    @Autowired
    private OrdersService ordersService;

    @PostMapping("/list")
    public CommonResult h5OrderListByPhone(@RequestBody QueryOrdersRequest ordersRequest) {
        log.info("===h5OrderListByPhone:{}", ordersRequest.getSerialNumber());
        if (StringUtils.isEmpty(ordersRequest.getSerialNumber())) {
            return CommonResult.success();
        }
        String tel = resolutionSmsToolsPhone(ordersRequest.getSerialNumber());
        log.info("===tel:{}", tel);
        if (StringUtils.isEmpty(tel)) {
            return CommonResult.success();
        }
        ordersRequest.setTel(tel);
        return CommonResult.success(ordersService.queryOrdersListByUserPhone(ordersRequest));
    }

    /**
     * 获取订单详情
     *
     * @return
     */
    @GetMapping("/orderInfoByOrderNo")
    public CommonResult getH5OrderInfoById(@RequestParam(value = "childOrderNo") String childOrderNo) {
        return CommonResult.success(ordersService.queryOrderInfoByNo(childOrderNo, getSource(), null));
    }

    /**
     * 解析短链的加密phone
     * @param phoneCode 密文手机号
     * @return  解密出来的手机号
     */
    private String resolutionSmsToolsPhone(String phoneCode) {
        try {
            phoneCode = phoneCode.replaceAll("%2B", "+");
            phoneCode = phoneCode.replaceAll("%3D", "=");
            phoneCode = phoneCode.replaceAll("%2F", "/");
            String phoneDeCode = Aes128CBCUtil.decrypt(phoneCode, "eCcgc+=F'=M)$+'+", "g&_&J{~mK~e#h_Y2");
            log.info("===解析短链手机号==={}", phoneDeCode);
            return phoneDeCode;
        } catch (Exception e) {
            log.error("===解析短链手机号异常==={}", e);
        }
        return null;
    }

}
